<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      python/前端/15-JQuery选择器 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>python/前端/15-JQuery选择器</h2>



  <p class="post-date">2020-11-03</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="选择器"><a href="#选择器" class="headerlink" title="选择器"></a>选择器</h1><p>通过选择器，可以获取到页面元素。jQuery具有强大的选择器，跟CSS3选择器类似</p>
<h2 id="1-基本选择器"><a href="#1-基本选择器" class="headerlink" title="1 基本选择器"></a>1 基本选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#id			根据给定的ID匹配一个元素</span><br><span class="line">element		根据给定的元素标签名匹配所有元素</span><br><span class="line">.class		根据给定的css类名匹配元素。</span><br><span class="line">*			匹配所有元素</span><br><span class="line">selector1,selector2,selectorN	将每一个选择器匹配到的元素合并后一起返回</span><br></pre></td></tr></table></figure>

<h2 id="2-层级选择器"><a href="#2-层级选择器" class="headerlink" title="2 层级选择器"></a>2 层级选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ancestor descendant	在给定的祖先元素下匹配所有的后代元素</span><br><span class="line">parent&gt;child		在给定的父元素下匹配所有的子元素</span><br><span class="line">prev+next			匹配所有紧接在 prev 元素后的 next 元素</span><br><span class="line">prev~siblings		匹配 prev 元素之后的所有 siblings 元素</span><br></pre></td></tr></table></figure>

<h2 id="3-过滤选择器"><a href="#3-过滤选择器" class="headerlink" title="3 过滤选择器"></a>3 过滤选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">:first			获取第一个元素</span><br><span class="line">:not(selector)	去除所有与给定选择器匹配的元素</span><br><span class="line">:even			匹配所有索引值为偶数的元素，从 0 开始计数</span><br><span class="line">:odd			匹配所有索引值为奇数的元素，从 0 开始计数</span><br><span class="line">:eq(index)		匹配一个给定索引值的元素</span><br><span class="line">:gt(index)		匹配所有大于给定索引值的元素</span><br><span class="line">:lang       	选择指定语言的所有元素。1.9+</span><br><span class="line">:last			获取最后个元素</span><br><span class="line">:lt(index)		匹配所有小于给定索引值的元素</span><br><span class="line">:header			匹配如 h1, h2, h3之类的标题元素</span><br><span class="line">:animated		匹配所有正在执行动画效果的元素</span><br><span class="line">:focus			匹配当前获取焦点的元素</span><br><span class="line">:root       	选择该文档的根元素   1.9+</span><br><span class="line">:target     	选择由文档URI的格式化识别码表示的目标元素    1.9</span><br></pre></td></tr></table></figure>

<h2 id="4-内容选择器"><a href="#4-内容选择器" class="headerlink" title="4 内容选择器"></a>4 内容选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:contains(text)	匹配包含给定文本的元素</span><br><span class="line">:empty			匹配所有不包含子元素或者文本的空元素</span><br><span class="line">:has(selector)	匹配含有选择器所匹配的元素的元素</span><br><span class="line">:parent			匹配含有子元素或者文本的元素</span><br></pre></td></tr></table></figure>

<h2 id="5-可见性选择器"><a href="#5-可见性选择器" class="headerlink" title="5 可见性选择器"></a>5 可见性选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:hidden		匹配所有不可见元素，或者type为hidden的元素</span><br><span class="line">:visible	匹配所有的可见元素</span><br></pre></td></tr></table></figure>

<h2 id="6-属性选择器"><a href="#6-属性选择器" class="headerlink" title="6 属性选择器"></a>6 属性选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[attribute]			匹配包含给定属性的元素</span><br><span class="line">[attribute&#x3D;value]	匹配给定的属性是某个特定值的元素</span><br><span class="line">[attribute!&#x3D;value]	匹配所有不含有指定的属性，或者属性不等于特定值的元素</span><br><span class="line">[attribute^&#x3D;value]	匹配给定的属性是以某些值开始的元素</span><br><span class="line">[attribute$&#x3D;value]	匹配给定的属性是以某些值结尾的元素</span><br><span class="line">[attribute*&#x3D;value]	匹配给定的属性是以包含某些值的元素</span><br><span class="line">[attrSel1][attrSel2][attrSelN]	复合属性选择器，需要同时满足多个条件时使用</span><br></pre></td></tr></table></figure>

<h2 id="7-子元素选择器"><a href="#7-子元素选择器" class="headerlink" title="7 子元素选择器"></a>7 子元素选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">:first-child		匹配所给选择器( :之前的选择器)的第一个子元素</span><br><span class="line">:first-of-type  	结构化伪类，匹配E的父元素的第一个E类型的孩子		1.9+</span><br><span class="line">:last-child			匹配最后一个子元素</span><br><span class="line">:last-of-type   	结构化伪类，匹配E的父元素的最后一个E类型的孩子	1.9+</span><br><span class="line">:nth-child()		匹配其父元素下的第N个子或奇偶元素		</span><br><span class="line">:nth-last-child()	选择所有他们父元素的第n个子元素。计数从最后一个元素开始到第一个	1.9+</span><br><span class="line">:nth-last-of-type()	选择的所有他们的父级元素的第n个子元素，计数从最后一个元素到第一个	1.9+</span><br><span class="line">:nth-of-type()  	选择同属于一个父元素之下，并且标签名相同的子元素中的第n个	1.9+</span><br><span class="line">:only-child			如果某个元素是父元素中唯一的子元素，那将会被匹配</span><br><span class="line">:only-of-type  		选择所有没有兄弟元素，且具有相同的元素名称的元素	1.9+</span><br></pre></td></tr></table></figure>

<h2 id="8-表单选择器"><a href="#8-表单选择器" class="headerlink" title="8 表单选择器"></a>8 表单选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">:input			匹配所有 input, textarea, select 和 button 元素</span><br><span class="line">:text			匹配所有的单行文本框</span><br><span class="line">:password		匹配所有密码框</span><br><span class="line">:radio			匹配所有单选按钮</span><br><span class="line">:checkbox		匹配所有复选框</span><br><span class="line">:submit			匹配所有提交按钮，匹配 type&#x3D;&quot;submit&quot; 的input或者button</span><br><span class="line">:image			匹配所有图像域</span><br><span class="line">:reset			匹配所有重置按钮</span><br><span class="line">:button			匹配所有按钮</span><br><span class="line">:file			匹配所有文件域</span><br></pre></td></tr></table></figure>

<h2 id="9-表单对象选择器"><a href="#9-表单对象选择器" class="headerlink" title="9 表单对象选择器"></a>9 表单对象选择器</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">:enabled		匹配所有可用元素</span><br><span class="line">:disabled		匹配所有不可用元素</span><br><span class="line">:checked		匹配所有选中的被选中元素(复选框、单选框等，select中的option)</span><br><span class="line">:selected		匹配所有选中的option元素</span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/%E5%89%8D%E7%AB%AF/17-JQuery%E6%93%8D%E4%BD%9CDOM/">
        <span class="nav-arrow">← </span>
        
          python/前端/17-JQuery操作DOM
        
      </a>
    
    
      <a class="nav-right" href="/python/%E5%89%8D%E7%AB%AF/2-HTML5%E5%B8%B8%E7%94%A8%E6%A0%87%E7%AD%BE/">
        
          python/前端/2-HTML5常用标签
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#选择器"><span class="toc-nav-text">选择器</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-基本选择器"><span class="toc-nav-text">1 基本选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-层级选择器"><span class="toc-nav-text">2 层级选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-过滤选择器"><span class="toc-nav-text">3 过滤选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-内容选择器"><span class="toc-nav-text">4 内容选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#5-可见性选择器"><span class="toc-nav-text">5 可见性选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#6-属性选择器"><span class="toc-nav-text">6 属性选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-子元素选择器"><span class="toc-nav-text">7 子元素选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#8-表单选择器"><span class="toc-nav-text">8 表单选择器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#9-表单对象选择器"><span class="toc-nav-text">9 表单对象选择器</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/前端/15-JQuery选择器/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "python/前端/15-JQuery选择器",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>